package com.huzy.smalloop.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huzy.smalloop.entity.SysUser;
import com.huzy.smalloop.service.ISysUserService;
import com.huzy.smalloop.utils.RsaUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.AbstractPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 系统登录操作
 * @Author: ziyin.hu
 * @Date: 2024-05-22 17:38
 * @Version:
 */

@RestController
@Tag(name = "系统登录模块")
@RequiredArgsConstructor
@RequestMapping("/sysLogin")
public class SysLoginController {

    private final ISysUserService sysUserService;

    private final PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public SaResult login(@RequestBody SysUser sysUser){
        LambdaQueryWrapper<SysUser> queryWrapperU = new LambdaQueryWrapper<>();
        queryWrapperU.eq(SysUser::getUsername,sysUser.getUsername());
        List<SysUser> list = sysUserService.list(queryWrapperU);
        if (list != null && list.size() > 0) {
            StpUtil.login(sysUser.getUserId());
            return SaResult.ok("登录成功");
        } else {
            return SaResult.error("fail login");
        }
    }

    @PostMapping("/encode")
    @Operation(summary = "加密")
    public Boolean test(){
        String s = "$10$yz0eS8Er5LwJTtHB01Rdpeyb0q/f7Hh1nY0qp1BIJ1ZG.Qm9ZF8WC";
        String encode = this.passwordEncoder.encode("Passw0rd@");
        System.out.println(encode);
        return passwordEncoder.matches("sQoY+lMVbKEj",s);
    }

}
